﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.PC.基础数据
{
    /// <summary>
    /// 货架
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class ShelfController : BaseController
    {
        private readonly IShelfService shelfService;
        public ShelfController(IWebHostEnvironment environment, IShelfService shelfService) : base(environment)
        {
            this.shelfService = shelfService;
        }
        /// <summary>
        /// 添加货架
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddShel(Shelf obj)
        {
            if (string.IsNullOrEmpty(obj.Code) || obj.Code.Length <= 0)
                return Ok(BaseResult.Result("400", "Id有误！"));

            string message = "";
           
            obj.CreateUserId = base.BaseGetNowUserId();
            obj.CreateUserName = base.BaseGetNowUserName();
            bool IsOK = shelfService.AddShel(obj, out message);
            if (IsOK)
            {       
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 显示货架信息
        /// </summary>
        /// <param name="hname"></param>
        /// <param name="kid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShelList(string? hname = "", long kid = 0, int pageIndex = 1, int pageSize = 20)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = shelfService.GetShelList(hname, kid, pageIndex, pageSize, out totalCount);
            if (list != null)
                return Ok(BaseResult.Result(list, "获取成功！", "200", totalCount));
            return Ok(BaseResult.Result(null, "获取失败！", "500", 0));
        }
        /// <summary>
        /// 删除货架信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelShel(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = shelfService.DelShel(id);
            if (IsOK)
            {          
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
        /// <summary>
        /// 根据库区获取货架
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAreaShelf(long aid = 0)
        {
            if (aid <= 0)
            {
                return Ok(BaseResult.Result("400", "库区Id有误！"));
            }
            var list = shelfService.GetAreaShelf(aid);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "获取成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500", 0));
        }
        /// <summary>
        /// 批量删除货架信息
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelShelList(long[] idList)
        {
            if (idList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            foreach (var uid in idList)
            {
                daRes = shelfService.DelShel(uid);                  
            }
            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
        /// <summary>
        /// 修改货架信息
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateShel(Shelf m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(m.Code))
            {
                return Ok(BaseResult.Result("400", "货架编码不能为空！"));
            }
            string message = "";

            m.ModifyUserId = base.BaseGetNowUserId();
            m.ModifyUserName = base.BaseGetNowUserName();
            var IsOK = shelfService.UpdShel(m, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！" + message, "500"));
        }
        /// <summary>
        /// 根据id获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShelById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var sobj = shelfService.GetShelById(id);
            if (sobj != null)
            {
                return Ok(BaseResult.Result(sobj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 添加仓位显示库房库区货架
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetWarAreaShellList()
        {
            var list = shelfService.GetWarAreaShellList();

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        ///// <summary>
        ///// 枚举货架类型
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public IActionResult GetEmShelfType()
        //{
        //    var list = BaseEnum.GetCodeRulesList<EmShelfType>();

        //    return Ok(list);
        //}
    }
}
